package com.xyh.flowershop.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyh.flowershop.context.WebContext;
import com.xyh.flowershop.domain.Comment;
import com.xyh.flowershop.domain.Tip;
import com.xyh.flowershop.mapper.CommentMapper;
import com.xyh.flowershop.mapper.TipMapper;
import com.xyh.flowershop.service.TipService;
import com.xyh.flowershop.vo.req.tip.TipAddReqVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author 小约翰可汗
 * @className TipServiceImpl
 * @createTime 2024/7/717:20
 */
@Service
public class TipServiceImpl extends ServiceImpl<TipMapper, Tip> implements TipService {

    private final CommentMapper commentMapper;

    public TipServiceImpl(CommentMapper commentMapper) {
        this.commentMapper = commentMapper;
    }

    @Override
    @Transactional
    public Boolean submitTip(TipAddReqVo vo) {
//        Tip tip = formTip(vo, WebContext.get());
        return baseMapper.insert(formTip(vo, WebContext.get())) > 0;
    }

    private Tip formTip(TipAddReqVo vo,String userId) {
        Tip tip = new Tip();
        Comment comment = commentMapper.selectById(vo.getCommentId());
        tip.setTipUserId(userId);
        BeanUtils.copyProperties(vo, tip);
        tip.setCreateTime(new Date());
        tip.setReportedUserId(comment.getUserId());
        return tip;
    }
}
